//
//  DetailViewController.m
//  MyProject
//
//  Created by apple on 15/11/2.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "DetailViewController.h"
#import "UIImageView+AFNetworking.h"
#import "ChildModel.h"
#import "TianQiViewController.h"
#import "GongLueViewController.h"
#import "KouDaiViewController.h"
#import "AppDelegate.h"






@interface DetailViewController ()<UITableViewDataSource,UITableViewDelegate>

{
    UITableView *_tableView;
   // FMDBDataBaseManager *_manager;
    
}
//做一个标记是否被收藏
@property (nonatomic, assign) BOOL isCache;
//@property (nonatomic, strong) ChildModel*model;

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isCache = YES;
  
    [self createView];
   // NSLog(@"%ld",self.arr.count);
    
    [self createNavigationBar:9];
    self.back.text = @"详情";
}

-(void)createView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64) style:0];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //注册
    [_tableView registerNib:[UINib nibWithNibName:@"DetailCell" bundle:nil] forCellReuseIdentifier:@"234"];
}

#pragma mark 代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChildModel *model = [self.arr objectAtIndex:indexPath.row];
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"234"];
    UIImageView * imgV = (UIImageView *)[cell.contentView viewWithTag:1000];
    [imgV setImageWithURL:[NSURL URLWithString:model.image_url]];
    
    UILabel * lable1 = (UILabel *)[cell.contentView viewWithTag:1001];
    lable1.text = model.name_zh_cn;
    UILabel * lable2 = (UILabel *)[cell.contentView viewWithTag:1002];
    lable2.text = model.name_en;
    
    UILabel * lable3 = (UILabel *)[cell.contentView viewWithTag:1003];
    lable3.text = [NSString stringWithFormat:@"%ld篇游记",model.destination_trips_count];
    
    //天气button
    UIButton * tianQi = (UIButton *)[cell.contentView viewWithTag:1006];
    [tianQi setTitle:[NSString stringWithFormat:@"%@",model.id] forState:UIControlStateNormal];
    //tianQi.titleLabel.text = model.id;
    tianQi.titleLabel.textColor = [UIColor clearColor];
    
    [tianQi addTarget:self action:@selector(pushTiqnQiView:) forControlEvents:UIControlEventTouchUpInside];
    
    //攻略按钮
    UIButton * gongLue = (UIButton *)[cell.contentView viewWithTag:1004];
    gongLue.titleLabel.text = model.id;
    gongLue.titleLabel.textColor = [UIColor clearColor];
    
    [gongLue addTarget:self action:@selector(pushGongLueView:) forControlEvents:UIControlEventTouchUpInside];
    
    //口袋书按钮
    UIButton * kouDai = (UIButton *)[cell.contentView viewWithTag:1005];
    kouDai.titleLabel.text = model.id;
    kouDai.titleLabel.textColor = [UIColor clearColor];
    
    [kouDai addTarget:self action:@selector(pushkouDaiView:) forControlEvents:UIControlEventTouchUpInside];
    
    
//    //收藏按钮
//    UIButton * shouCang = (UIButton *)[cell.contentView viewWithTag:1100];
//   
//   
//    [shouCang addTarget:self action:@selector(shouCangSelected:) forControlEvents:UIControlEventTouchUpInside];
//    
//   //将model的id 赋值给button的title
//    shouCang.titleLabel.text = model.id;
//    
//    //shouCang.selected = NO;
//    
//    shouCang.titleLabel.textColor = [UIColor clearColor];
//   
//    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    [shouCang setTitle:model.id forState:UIControlStateNormal];
//    shouCang.titleLabel.textColor = [UIColor clearColor];
//    
//    
//    for (int i = 0; i< delegate.arr.count; i++) {
//        NSString * str = [delegate.arr objectAtIndex:i];
//        if ([str isEqualToString:model.id]) {
//            shouCang.selected = YES;
//            return cell;
//        }
//
//    }
//    [shouCang setImage:[UIImage imageNamed:@"shouCang2-Small.png"] forState:UIControlStateNormal];
//    shouCang.selected = NO;
//    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 250;
}
 //点击了某个cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ChildModel *model = [self.arr objectAtIndex:indexPath.row];
    GongLueViewController * gongLue = [[GongLueViewController alloc]init];
    gongLue.name = model.name_zh_cn;
    gongLue.ID = [NSString stringWithFormat:@"%@",model.id];
    [self.navigationController pushViewController:gongLue animated:YES];
    
}

////收藏
//-(void)shouCangSelected:(UIButton *)button
//{
//    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    //说明要取消收藏
//    if (button.selected) {
//        
//        ChildModel *model = [self.arr objectAtIndex:button.tag - 100];
//        button.selected = NO;
//        [_manager deleteDataWithModel:model];
//
//        [delegate.arr removeObject:model.id];
//        
//        [_idarr removeObject:button.titleLabel.text];
//        button.selected = NO;
//    }else
//    {
////        NSLog(@"设置被选中图片");
////         ChildModel *model = [self.arr objectAtIndex:button.tag - 100];
//        [button setImage:[UIImage imageNamed:@"shouCangSelected-Small.png"] forState:UIControlStateNormal];
//        //加入收藏
//        button.selected = YES;
//        [delegate.arr addObject:button.titleLabel.text];
//      
//        
//    }
//}
//
//







////隐藏多余的cell线
//- (void)hideTableViewBottomLine {
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
//    view.backgroundColor = [UIColor clearColor];
//    _tableView.tableFooterView = view;
//}

//天气按钮
-(void)pushTiqnQiView:(UIButton *)btn
{
    TianQiViewController * tianQi = [[TianQiViewController alloc]init];
    tianQi.ID = btn.titleLabel.text;
    
    [self.navigationController pushViewController:tianQi animated:YES];
}
//攻略
-(void)pushGongLueView:(UIButton *)btn
{
    GongLueViewController * gongLue = [[GongLueViewController alloc]init];
    gongLue.ID = btn.titleLabel.text;
    
    [self.navigationController pushViewController:gongLue animated:YES];
}

//口袋书按钮
-(void)pushkouDaiView:(UIButton *)btn
{
    KouDaiViewController * kouDai = [[KouDaiViewController alloc]init];
   
    kouDai.ID = btn.titleLabel.text;
    
    [self.navigationController pushViewController:kouDai animated:YES];

    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
